home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 November / PCWNOV08.iso / Software / Freeware / NoScript 1.7.7 / noscript-1.7.7-fx+mz+sm.xpi / chrome / noscript.jar / content / noscript / iaUI.js < prev    next >
Encoding:
Text File  |  2008-07-14  |  4.5 KB  |  155 lines

  1. function UIUtils(serv) {
  2.   this.serv = serv;
  3. }
  4. UIUtils.prototype = {
  5.   tabselPrefName: "options.tabSelectedIndexes",
  6.   resumeTabSelections: function() {
  7.     var indexes = window.arguments && window.arguments[0] && window.arguments[0].tabselIndexes ||
  8.       this.serv.getPref(this.tabselPrefName, "").split(/\s*,\s*/);
  9.     // select tabs from external param
  10.  
  11.     var tabs = document.getElementsByTagName("tabs");
  12.     var tcount = Math.min(tabs.length, indexes.length);
  13.     var listener = function(ev) { arguments.callee.binding.persistTabSelections(); }
  14.     listener.binding = this;
  15.     for(var t = tabs.length; t-- > 0;) {
  16.       try {
  17.         tabs[t].selectedIndex = parseInt(indexes[t]) || 0;
  18.       } catch(e) {}
  19.       tabs[t].addEventListener("select", listener, false); 
  20.     }
  21.     this.persistTabSelections();
  22.   },
  23.   
  24.   persistTabSelections: function() {
  25.      var tabs = document.getElementsByTagName("tabbox");
  26.      var ss = [];
  27.      for(var tcount = 0; tcount < tabs.length; tcount++) {
  28.        ss.push(tabs[tcount].selectedIndex);
  29.      }
  30.      this.serv.setPref(this.tabselPrefName, ss.join(","));
  31.   },
  32.   
  33.   visitCheckboxes: function(callback) {
  34.     const rxOpt=/^(inv|moz|)opt-(.*)/;
  35.     var j, checkbox, match;
  36.     const opts = document.getElementsByTagName("checkbox");
  37.     for(j = opts.length; j-- > 0;) {
  38.       checkbox = opts[j];
  39.       if((match = checkbox.id.match(rxOpt))) {
  40.         callback(match[2], match[1] == "inv", checkbox, match[1] == "moz");
  41.       }
  42.     }
  43.   }
  44. };
  45.  
  46.  
  47. function ConditionalGroup(serv, prefName, def) {
  48.   this.serv = serv;
  49.   this.prefName = prefName;
  50.   this.cbx = document.getElementById("cbx-" + prefName);
  51.   this.sel = document.getElementById("sel-" + prefName);
  52.   var value = this.serv.getPref(prefName, def);
  53.   this.defaultIndex = typeof(def) == "number" ? def : 0;
  54.   this.cbx.checked =  !!value;
  55.   this.sel.selectedIndex = value ? value - 1 : def;
  56.   var instance = this;
  57.   this.cbx.conditionalGroup = this;
  58.   this.cbx.setAttribute("oncommand", "this.conditionalGroup.changed()");
  59.   this.changed();
  60. }
  61.  
  62. ConditionalGroup.prototype = {
  63.   changed: function() {
  64.     this.sel.disabled = !this.cbx.checked;
  65.     if(this.cbx.checked && this.sel.selectedIndex < 0) {
  66.       this.sel.selectedIndex = this.defaultIndex;
  67.     }
  68.   },
  69.   getValue: function() {
  70.     return this.cbx.checked && this.sel.selectedIndex + 1 || 0;
  71.   },
  72.   persist: function() {
  73.     this.serv.setPref(this.prefName, this.getValue());
  74.   }
  75. };
  76.  
  77. function SoundChooser(id, title, serv, def) {
  78.   this.id = id;
  79.   this.title = title;
  80.   this.serv = serv;
  81.   this.def = def;
  82. }
  83.  
  84. SoundChooser.prototype = {
  85.   choose: function(btn) {
  86.     try {
  87.       const cc=Components.classes;
  88.       const ci=Components.interfaces;
  89.       const fp = cc["@mozilla.org/filepicker;1"].createInstance(ci.nsIFilePicker);
  90.       
  91.       fp.init(window,title, ci.nsIFilePicker.modeOpen);
  92.       fp.appendFilter(this.serv.getString("audio.samples"),"*.wav");
  93.       fp.filterIndex=0;
  94.       const ret=fp.show();
  95.       if (ret==ci.nsIFilePicker.returnOK || ret==ci.nsIFilePicker.returnReplace) {
  96.         this.setSample(fp.fileURL.spec);
  97.         this.play();
  98.       }
  99.     } catch(ex) {
  100.       noscriptUtil.prompter.alert(window, title, ex.toString());
  101.     }
  102.   },
  103.   setSample: function(url) {
  104.     document.getElementById(this.id).value = url || this.def;
  105.   },
  106.   getSample: function() {
  107.     return document.getElementById(this.id).value;
  108.   },
  109.   play: function() {
  110.     this.serv.playSound(this.getSample(), true);
  111.   }
  112. };
  113.  
  114. function RegExpController(prefix, parseMethod, value) {
  115.   this.parse = parseMethod || this.parse;
  116.   this.regexp = document.getElementById(prefix + "-regexp");
  117.   this.sample = document.getElementById(prefix + "-sample");
  118.   var listener = function(ev) { arguments.callee.binding.feedback(); };
  119.   listener.binding = this;
  120.   this.regexp.addEventListener("input", listener, false);
  121.   this.sample.addEventListener("input", listener, false);
  122.   this.regexp.value = value;
  123.   this.feedback();
  124. }
  125.  
  126. RegExpController.prototype = {
  127.   parse: function(s) { return new RegExp(s, "g"); },
  128.   validate: function() {
  129.     const textbox = this.regexp;
  130.     try {
  131.       const rx = this.parse(textbox.value);
  132.       if(rx) {
  133.         textbox.className = "";
  134.         return rx;
  135.       }
  136.     } catch(e) {}
  137.     textbox.className = "noscript-error";
  138.     return null;
  139.   },
  140.   feedback: function() {
  141.     const rx = this.validate();
  142.     const sample = this.sample; 
  143.     if(rx && rx.test(sample.value)) {
  144.       sample.className = "";
  145.     } else {
  146.       sample.className = "noscript-error";
  147.     }
  148.     return rx;
  149.   },
  150.   getValue: function(valid) {
  151.     if(valid && !this.validate()) return null;
  152.     return this.regexp.value;
  153.   }
  154. };
  155.